#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
// #include <lib/common.h>
#include <errno.h>

size_t readn(int fd, void *buf, size_t size)
{
    char *buf_point = buf;
    int length = size;
    while (length > 0)
    {
        int result = read(fd, buf_point, length);
        if (result < 0)
        {
            if (errno == EINTR)
            {
                // 考虑非阻塞的情况 再次调用read
                continue;
            }
            else
            {
                return -1;
            }
        }
        else if (result == 0)
        {
            // EOF(End of File)表示套接字关闭
            break;
        }

        length -= result;
        buf_point += result;
    }

    return size - length;
}